package com.neusoft.neuiotms.dcom.service.problem.impl;

import javax.persistence.EntityNotFoundException;
import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.activiti.dto.ProcessInstanceDTO;
import com.neusoft.bizcore.activiti.enums.WorkOrderStatus;
import com.neusoft.bizcore.activiti.service.ProcessInstanceService;
import com.neusoft.bizcore.activiti.service.WorkOrderService;
import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.file.service.FileService;
import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.neuiotms.dcom.converter.problem.ProblemRegistrationConverter;
import com.neusoft.neuiotms.dcom.dto.problem.ProblemRegistrationDTO;
import com.neusoft.neuiotms.dcom.model.problem.ProblemRegistration;
import com.neusoft.neuiotms.dcom.repository.problem.ProblemRegistrationAttachmentRepository;
import com.neusoft.neuiotms.dcom.repository.problem.ProblemRegistrationRepository;
import com.neusoft.neuiotms.dcom.service.problem.ProblemRegistrationService;

@Service
public class ProblemRegistrationServiceImpl implements ProblemRegistrationService {

    @Autowired
    private ProblemRegistrationRepository problemRegistrationRepository;

    @Autowired
    private ProblemRegistrationConverter problemRegistrationConverter;

    @Autowired
    private ProblemRegistrationAttachmentRepository problemRegistrationAttachmentRepository;

    @Autowired
    private FileService fileService;

    @Autowired
    private WorkOrderService workOrderService;

    @Autowired
    private ProcessInstanceService processInstanceService;

    @Override
    public ProblemRegistrationDTO create(final ProblemRegistrationDTO dto, final UserBean user) {
        if (WorkOrderStatus.temporary.name().equals(dto.getStatus())) {
            final String workOrderNum =
                    this.workOrderService.temporaryStorage(dto.getTitle(), "problem", dto.getWorkOrderNum(), user);
            dto.setWorkOrderNum(workOrderNum);
        } else {
            final ProcessInstanceDTO instance =
                    this.workOrderService.submit(dto.getTitle(), "problem", dto.getWorkOrderNum(), user);
            dto.setTaskId(instance.getTaskId());
            dto.setWorkOrderNum(instance.getWorkorderNum());

            // 设置流程变量
            this.processInstanceService.setGlobalVariable(instance.getTaskId(), "zpexecutor",
                    dto.getExecutorCode());
            // 完成节点
            this.processInstanceService.complete(dto.getWorkOrderNum(), instance.getTaskId(), user);
        }

        // 查询当前workOrderNum和TaskId下是否存在记录
        final ProblemRegistration oldModel = this.problemRegistrationRepository.findByWorkOrderNumAndTaskId(
                dto.getWorkOrderNum(), dto.getTaskId());
        if (oldModel == null) { // 如果不存在新增
            dto.setId(null);
        }

        final ProblemRegistration model = this.problemRegistrationConverter.toModel(dto);
        this.problemRegistrationRepository.save(model);
        return this.problemRegistrationConverter.toDTO(model);
    }

    @Override
    public ProblemRegistrationDTO detail(final String workOrderNum, final String taskId, final UserBean user) {
        ProblemRegistration model = null;
        try {
            if (StringUtils.isNotBlank(workOrderNum) && !"NULL".equals(taskId)) {
                model = this.problemRegistrationRepository.findByWorkOrderNumAndTaskId(workOrderNum, taskId);
                if (model == null) {
                    // 获取前一节点的TaskID
                    final String lastTaskId = this.processInstanceService.getLastTaskId(taskId, user);
                    if (lastTaskId == null) {
                        return new ProblemRegistrationDTO();
                    } else {
                        model = this.problemRegistrationRepository.findByWorkOrderNumAndTaskId(workOrderNum,
                                lastTaskId);
                    }
                }
            } else if ("NULL".equals(taskId)) {
                model = this.problemRegistrationRepository.findByWorkOrderNum(workOrderNum);
            }
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND,
                    new Object[] {workOrderNum },
                    new Object[] {taskId });
        }
        return this.problemRegistrationConverter.toDTO(model);
    }

    @Override
    public ProblemRegistrationDTO findByWorkOrderNumAndTaskId(final String workOrderNum, final String taskId) {
        final ProblemRegistration model =
                this.problemRegistrationRepository.findByWorkOrderNumAndTaskId(workOrderNum, taskId);
        return this.problemRegistrationConverter.toDTO(model);
    }

    @Override
    @Transactional
    public void deleteMongoFile(final String fsId) {
        this.fileService.deleteByFsId(fsId);
        this.problemRegistrationAttachmentRepository.deleteByFsId(fsId);
    }

    @Override
    public void deleteByWorkOrderNum(final String workOrderNum) {
        this.problemRegistrationRepository.deleteByWorkOrderNum(workOrderNum);
    }

    @Override
    public ProblemRegistrationDTO findByWorkNum(final String workOrderNum) {
        final ProblemRegistration model = this.problemRegistrationRepository.findByWorkNum(workOrderNum);
        if (model != null) {
            return this.problemRegistrationConverter.toDTO(model);
        } else {
            return null;
        }
    }

}
